import request from "@/service/request";
import {PromisePageResponse, PromiseResponse} from "@/service/api/type.ts";
import {PermissionNode, SysModelVo, SysRoleVo} from "@/service/api/system/rolePermission/types.ts";

/**
 * 获取权限树
 */
const getPermissionTree = (): PromiseResponse<PermissionNode[]> => {
  return request({
    url: "/system/permission/treeselect",
    method: "get",
  });
};

/**
 * 获取角色列表
 */
const getRoleList = (query: any): PromisePageResponse<SysRoleVo> => {
  return request({
    url: "/system/role/list",
    method: "get",
    params: query,
  });
};

/**
 * 将权限绑定到角色
 * @param query
 */
const addPermission = (query: any): PromiseResponse<any> => {
  return request({
    url: "/system/role/addRolePermission",
    method: "post",
    data: query,
  });
};

/**
 * 获取角色的权限
 * @param roleId
 */
const getRolePermission = (roleId: string): PromiseResponse<SysRoleVo> => {
  return request({
    url: `/system/role/${roleId}`,
    method: "get",
  });
};

/**
 * 获取模型字段
 * @param modelId
 */
const getFields = (modelId: string): PromiseResponse<SysModelVo> => {
  return request({
    url: `/system/model/${modelId}`,
    method: "get",
  });
};

/**
 * 保存可见模型字段
 * @param query
 */
const saveFields = (query: any): PromiseResponse<any> => {
  return request({
    url: "/system/role/editRolePermission",
    method: "put",
    data: query,
  });
};

/**
 * 获取已经分配的模型字段
 * @param query
 */
const getSavedFields = (query: any): PromiseResponse<SysModelVo> => {
  return request({
    url: "/system/model/allocatedModel",
    method: "get",
    params: query,
  });
};

/**
 * 取消勾选
 * @param query
 */
const delPermission = (query: any): PromiseResponse<any> => {
  return request({
    url: "/system/role/cancelRolePermission",
    method: "put",
    data: query,
  });
};

/**
 * 为角色保存权限
 * @param roleId
 * @param query
 */
const savePermission = (roleId: string, query: any): PromiseResponse<any> => {
  return request({
    url: `/system/role/fullAmountEditRolePermission/${roleId}`,
    method: "post",
    data: query,
  });
};

export {
  getPermissionTree,
  getRoleList,
  addPermission,
  getRolePermission,
  getFields,
  saveFields,
  getSavedFields,
  delPermission,
  savePermission,
};
